package com.bpms.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream; 
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件操作类
 * @author menlin
 */
public class FileOperation {
	
	public static final String UTF8 = "UTF-8";
	public static final String gb2312 = "gb2312";
	public static final String GBK = "GBK";
	public static final String ISO = "ISO-8859-1";
	
	protected File file;
	
	public FileOperation(File file) {
		this.file = file;
	}
	
	/**
	 * @return 文件大小
	 * @exception FileOperationException
	 */
	public int getSize() throws FileOperationException {
		InputStream in = null;
		
		try {
			in = createBufferedInputStream();
			return in.available();
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileOperationException("检查文件大小时发生异常。", e);
		} finally {
			closeable(in);
		}
	}
	
	/**
	 * 获得文件内容
	 * @return
	 * @throws FileOperationException
	 */
	public String getContent() throws FileOperationException {
		BufferedReader reader = null;
		
		try {
			reader = createBufferedReader();
			StringBuilder content = new StringBuilder();
			for (String line = reader.readLine(); line != null; line = reader.readLine()) {
				content.append(line);
			}
			return content.toString();
		} catch(IOException e) {
			e.printStackTrace();
			throw new FileOperationException("获取文件内容时发生错误。", e);
		} finally {
			closeable(reader);
		}
	}
	/**
	 * 获得文件内容，每一行放在List中
	 * @return
	 * @throws FileOperationException
	 */
	public List<String> getContentByLine(String charsetName) throws FileOperationException {
		List<String> list = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			reader = createBufferedReader(charsetName);
			for (String line = reader.readLine(); line != null; line = reader.readLine()) {
				list.add(line);
			}
		} catch(IOException e) {
			e.printStackTrace();
			throw new FileOperationException("获取文件内容时发生错误。", e);
		} finally {
			closeable(reader);
		}
		return list;
	}
	
	/**
	 * 保存文件
	 * @param toFile
	 * @throws FileOperationException
	 */
	public void save(File toFile) throws FileOperationException {
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = createBufferedInputStream();
			out = createBufferedOutputStream(toFile);
			byte[] buffer = new byte[8192];
			// 存储文件
			while (read(in, buffer) > 0) {
				write(out, buffer);
			}
		} finally {
			closeable(in);
			closeable(out);
		}
	}
	
	/**
	 * 创建未见读取器
	 * @return
	 */
	protected BufferedReader createBufferedReader() {
		try {
			return new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件读取器时发生错误。", e);
		}
	}
	
	/**
	 * 根据指定字符集读取文件
	 * @param charsetName
	 * @return
	 */
	protected BufferedReader createBufferedReader(String charsetName) {
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charsetName);
			return new BufferedReader(isr);
		} catch (Exception e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件读取器时发生错误。", e);
		}
	}
	/**
	 * 创建未见读取器
	 * @return
	 */
	protected BufferedReader createBufferedReader(CharsetDecoder dec) {
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), dec);
			return new BufferedReader(isr);
		} catch (Exception e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件读取器时发生错误。", e);
		}
	}
	/**
	 * 创建未见读取器
	 * @return
	 */
	protected BufferedReader createBufferedReader(Charset cs) {
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), cs);
			return new BufferedReader(isr);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件读取器时发生错误。", e);
		}
	}
	
	/**
	 * 创建输入流
	 * @param file
	 * @return
	 * @throws FileOperationException
	 */
	protected BufferedInputStream createBufferedInputStream() throws FileOperationException {
		try {
			return new BufferedInputStream(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件输入流时发生错误。", e);
		}
	}
	
	/**
	 * 创建输出流
	 * @param file
	 * @return
	 * @throws FileOperationException
	 */
	protected BufferedOutputStream createBufferedOutputStream(File file) throws FileOperationException {
		try {
			return new BufferedOutputStream(new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileOperationException("创建文件输入流时发生错误。", e);
		}
	}
	
	/**
	 * 从输入流中读取下一个字节
	 * @param in
	 * @param buffer
	 * @return
	 * @throws FileOperationException
	 */
	protected int read(InputStream in, byte[] buffer) throws FileOperationException {
		try {
			return in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileOperationException("从输入流中读取下一个字节时发生错误。", e);
		}
	}
	
	/**
	 * 输出流写入
	 * @param out
	 * @param buffer
	 * @throws FileOperationException
	 */
	protected void write(OutputStream out, byte[] buffer) throws FileOperationException {
		try {
			out.write(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileOperationException("输出流写入时发生错误。", e);
		}
	}
	
	/**
	 * 关闭数据源或者目标
	 * @param source
	 */
	protected void closeable(Closeable source) {
		if (source != null) {
			try {
				source.close();
			} catch (IOException e) {
				e.printStackTrace();
				throw new FileOperationException("关闭数据源或者目标时发生异常。", e);
			}
		}
		
	}

	/**
	 * @return 文件
	 */
	public File getFile() {
		return file;
	}
	
	public static void main (String[] args) {
		String path = "e:/card_data";
		File f = new File(path);
		FileOperation fo = new FileOperation(f);
		List<String> list = fo.getContentByLine("gb2312");
		String s = list.get(2);
		System.out.println(s);
		String s2 = s.replace("|", ",");
		System.out.println(s2);
		String[] ss = s2.split(",");
//		String[] ss = s.split("|");
		System.out.println("ss.length="+ ss.length);
		for(int i=0;i<ss.length;i++){
			System.out.println("ss"+i+"=="+ss[i].trim());
		}
		System.out.println(list.size());
	}

}
